package m202405.d12;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/5/12 12:28
 */
public class Q189 {
    public void rotate1(int[] nums, int k) {
        k = k % nums.length;
        int[] kn = new int[k];
        for (int i = nums.length - k, j = 0; i < nums.length; i++, j++) {
            kn[j] = nums[i];
        }
        for (int length = nums.length - 1; length >= k; length--) {
            nums[length] = nums[length - k];
        }
        System.arraycopy(kn, 0, nums, 0, kn.length);
    }

    public void rotate(int[] nums, int k) {
        k = k % nums.length;
        reverse(nums, 0, nums.length - 1);
        reverse(nums, 0, k - 1);
        reverse(nums, k, nums.length - 1);
    }

    private void reverse(int[] nums, int start, int end) {
        while (start < end) {
            int temp = nums[start];
            nums[start] = nums[end];
            nums[end] = temp;
            start++;
            end--;
        }
    }
}
